package com.etcbase.dcs.controller.map;

import android.graphics.drawable.Drawable;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapSelectedLocationOverlay extends ItemizedOverlay<OverlayItem> {
	public static final String TAG = MapSelectedLocationOverlay.class.getSimpleName();
			
	private OverlayItem mSelectedLocationOverlayItem;
			
	// CONSTRUCTOR
	public MapSelectedLocationOverlay(	Drawable marker ) {
		super(boundCenterBottom(marker));
		mSelectedLocationOverlayItem = new OverlayItem(new GeoPoint(0,0), null, null);
		populate();
	}		
			
	public void setSelectedLocation( GeoPoint p ) {
		setSelectedLocation(p, null, null);
	}
	public void setSelectedLocation( GeoPoint p, String title, String snippet ) {
		mSelectedLocationOverlayItem = new OverlayItem(p, title, snippet);
		populate();
	}
			
	public GeoPoint getSelectedLocation() {
		return mSelectedLocationOverlayItem.getPoint();
	}
		
    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView){
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
			GeoPoint p = mapView.getProjection().fromPixels((int)event.getX(), (int)event.getY());
			setSelectedLocation(p);
			populate();				
        }	
        return super.onTouchEvent(event, mapView);
    }	            
    @Override
    protected boolean onTap(int index) {	    	
    	return super.onTap(index);
    }
	
	@Override
	protected OverlayItem createItem(int i) {
		return mSelectedLocationOverlayItem;
	}
	@Override
	public int size() {
		return 1;
	}
}